//
//  ActivityViewController.m
//  小汽车
//
//  Created by 杜远洋 on 14-12-29.
//  Copyright (c) 2014年 蓝欧科技. All rights reserved.
//

#import "ActivityViewController.h"
#import "ActivityTableViewCell.h"
#import "ActivityModel.h"
#import "ActivityPlusTableViewCell.h"
#import "ActivityPlusModel.h"
#import "UIImageView+WebCache.h"
#import "DetailActivityViewController.h"
#import "Masonry.h"
#import "AFJSONRequestOperation.h"
@interface ActivityViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation ActivityViewController

- (void)dealloc
{
    [_backbround release];
    [_tableArray release];
    [_tableView release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"活动";
    [self myConnection];
    
 
    
}

- (void)changeBackgroundColor
{
    NSUserDefaults *myNight = [NSUserDefaults standardUserDefaults];
    if ([[myNight objectForKey:@"night"] isEqualToString:@"0"]) {
        [self.view setBackgroundColor:[UIColor whiteColor]];
    }
    else
    {
        [self.view setBackgroundColor:[UIColor colorWithRed:54/255.0f green:54/255.0f blue:54/255.0f alpha:1]];
    }
}


-(void)myConnection
{
    NSString *str = [NSString stringWithFormat:@"http://wap.xcar.com.cn/cms/interface/5.0/getEvent.php"];
    [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

        [_tableArray removeAllObjects];
        NSMutableArray *array = [NSMutableArray array];
        [array addObjectsFromArray:JSON];
        for (NSDictionary *dic in array) {
            
            ActivityModel *model = [[ActivityModel alloc] init];
            ActivityPlusModel *models = [[ActivityPlusModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            
            [models setValuesForKeysWithDictionary:dic];
            [_tableArray addObject:model];
            [_tableArray addObject:models];
        }
        [self createView];
      
        [_tableView reloadData];
        
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"%@",error);
    }];
    [operation start];
    
}

-(void)createView
{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49) style:UITableViewStylePlain];

    self.tableView.backgroundColor = [UIColor clearColor];
    _tableView.rowHeight = _tableView.frame.size.height /2 - 50;
   
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[ActivityTableViewCell class] forCellReuseIdentifier:@"cell1"];
    [_tableView registerClass:[ActivityPlusTableViewCell class] forCellReuseIdentifier:@"cell2"];
    [self.view addSubview:_tableView];

    [_tableView release];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row % 2 == 0) {
        return _tableView.frame.size.height /2 - 30;
    }
    else{
        return 45;
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row % 2 == 0) {
        ActivityModel *model = [_tableArray objectAtIndex:indexPath.row];
        ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        cell.backgroundColor = [UIColor clearColor];
        cell.activityTitle.text = model.event_title;
        cell.activityTime2.text = model.event_endtime;
        cell.activitySite.text = model.event_place;
        
        //设置夜间模式
        NSUserDefaults *myNight = [NSUserDefaults standardUserDefaults];
        if ([[myNight objectForKey:@"night"] isEqualToString:@"0"]) {
            cell.time.textColor = [UIColor blackColor];
            cell.place.textColor = [UIColor blackColor];
            cell.activityTitle.textColor = [UIColor blackColor];
        }
        else
        {
            cell.time.textColor = [UIColor whiteColor];
            cell.place.textColor = [UIColor whiteColor];
            cell.activityTitle.textColor = [UIColor whiteColor];

        }

        [cell.activityImage setImageWithURL:[NSURL URLWithString:model.event_smallimg] placeholderImage:[UIImage imageNamed:@"placeHolder.png"] options:SDWebImageRetryFailed];
        cell.highlighted = NO;
        
        
        cell.selectionStyle = UITableViewCellAccessoryNone;
        return cell;
    }else{
        ActivityPlusModel *model = [_tableArray objectAtIndex:indexPath.row];
        ActivityPlusTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        cell.backgroundColor = [UIColor clearColor];
        cell.activityType.text = model.event_status;
        if ([model.event_status  isEqual: @"1"]) {
            cell.activityType.text = @"活动进行中";
            cell.activityType.textColor = [UIColor colorWithRed:17.0/255.0 green:190.0/255.0 blue:227.0/255.0 alpha:1.0];
        }else if ([model.event_status isEqual:@"0"]){
            cell.activityType.text = @"活动已结束";
            cell.activityType.textColor = [UIColor colorWithRed:17.0/255.0 green:190.0/255.0 blue:227.0/255.0 alpha:1.0];
        }
        cell.interested.text = model.event_interest;
        
        //设置夜间模式
        NSUserDefaults *myNight = [NSUserDefaults standardUserDefaults];
        if ([[myNight objectForKey:@"night"] isEqualToString:@"0"]) {
           cell.inter.textColor = [UIColor blackColor];
        }
        else
        {
            cell.inter.textColor = [UIColor whiteColor];
            
        }

        cell.userInteractionEnabled = NO;
        cell.highlighted = NO;
        return cell;
    }
    
    
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailActivityViewController *detailActivity = [[DetailActivityViewController alloc] init];
    ActivityModel *model = [_tableArray objectAtIndex:indexPath.row];
    
    [self setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self.navigationController pushViewController:detailActivity animated:YES];
    detailActivity.event_link = model.event_link;
    detailActivity.event_title = model.event_title;
    [detailActivity release];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.tableView reloadData];
    [self changeBackgroundColor];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
